package com.aaa.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * @ClassName: JwtTokenUtils
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/8/31 11:52
 * @Version:1.0
 **/
public class JwtTokenUtils {
    private static final String ISS_USER="aaa";

    private static final long EXPIRATION_TIME=1000 * 60 * 10 * 10;

    private static final String SECRET = "jwt-token";

    public static String getToken(Map map) {
        String token = Jwts.builder()
//                签发者
                .setIssuer(ISS_USER)
//                面向的用户
                .setSubject("vue")
//                签发时间
                .setIssuedAt(new Date())
//                到期时间
                .setExpiration(new Date(System.currentTimeMillis()+ EXPIRATION_TIME))
//                签名算法和密钥
                .signWith(SignatureAlgorithm.HS256, SECRET)
//                相关信息
                .addClaims(map )
                .compact();
        return token;
    }
    public static Claims setToken(String token) {
        Claims jws=null;
        try {
            jws = Jwts.parser()
                    .setSigningKey(SECRET)
                    .parseClaimsJws(token)
                    .getBody();
        }catch (Exception e){
            e.printStackTrace();
        }
        return jws;
    }
    public static boolean isExpiration(String token) {
        Claims jws = setToken(token);

        return  jws.getExpiration().before(new Date());
    }
}
